home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / Make / source / amiga / amiga.c next >
Encoding:
C/C++ Source or Header  |  1997-09-25  |  6.7 KB  |  344 lines

  1. /* Running commands on Amiga
  2. Copyright (C) 1995, 1996 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "variable.h"
  21. #include "amiga.h"
  22. #include <assert.h>
  23. #include <exec/memory.h>
  24. #include <dos/dostags.h>
  25. #include <proto/exec.h>
  26. #include <proto/dos.h>
  27. #include <proto/icon.h>
  28.  
  29. #include <workbench/startup.h>
  30.  
  31. #include "wbpath.h"
  32.  
  33. #define PATH_SIZE       1024
  34.  
  35. char *variable_buffer_output();
  36.  
  37. extern void KPrintF(char *fmt,...);
  38.  
  39. static struct WBStartup *wbmsg = NULL;
  40. static const char Amiga_version[] = "$VER: Make 3.76.1 (24.09.97)\n"
  41.             "Amiga Port by\n"
  42.             "A. Digulla (digulla@home.lake.de) and\n"
  43.             "St. Ruppert (ruppert@amigaworld.com)\n";
  44.  
  45. /* SAS/C startup configuration */
  46. long __stack = 20000;
  47. char __stdiowin[] = "CON:////GNU make V3.76.1/AUTOOPEN/CLOSE/WAIT";
  48.  
  49. int
  50. MyExecute (argv)
  51. char ** argv;
  52. {
  53.     char * buffer, * ptr;
  54.     char ** aptr;
  55.     int len = 0;
  56.     int status;
  57.  
  58.     for (aptr=argv; *aptr; aptr++)
  59.     {
  60.     len += strlen (*aptr) + 4;
  61.     }
  62.  
  63.     buffer = xmalloc (len);
  64.  
  65.     ptr = buffer;
  66.  
  67.     for (aptr=argv; *aptr; aptr++)
  68.     {
  69.     if (((*aptr)[0] == ';' && !(*aptr)[1]))
  70.     {
  71.         *ptr ++ = '"';
  72.         strcpy (ptr, *aptr);
  73.         ptr += strlen (ptr);
  74.         *ptr ++ = '"';
  75.     }
  76.     else if ((*aptr)[0] == '@' && (*aptr)[1] == '@' && !(*aptr)[2])
  77.     {
  78.         *ptr ++ = '\n';
  79.         continue;
  80.     }
  81.     else
  82.     {
  83.         strcpy (ptr, *aptr);
  84.         ptr += strlen (ptr);
  85.     }
  86.     *ptr ++ = ' ';
  87.     *ptr = 0;
  88.     }
  89.  
  90.     ptr[-1] = '\n';
  91.  
  92.     if(wbmsg != NULL)
  93.     {
  94.     BPTR path = CloneWorkbenchPath(wbmsg);
  95.  
  96.     status = SystemTags(buffer,
  97.                 SYS_UserShell, TRUE,
  98.                 NP_Path, path,
  99.                 TAG_DONE);
  100.  
  101.     if(status == -1)
  102.         FreeWorkbenchPath(path); 
  103.     } else
  104.     status = SystemTags(buffer,
  105.                 SYS_UserShell, TRUE,
  106.                 TAG_END);
  107.  
  108.     free(buffer);
  109.  
  110.     if (SetSignal(0L,0L) & SIGBREAKF_CTRL_C)
  111.     status = 20;
  112.  
  113.     /* Warnings don't count */
  114.     if (status == 5)
  115.     status = 0;
  116.  
  117.     return status;
  118. }
  119.  
  120. char *
  121. wildcard_expansion (wc, o)
  122. char * wc, * o;
  123. {
  124.     struct AnchorPath * apath;
  125.  
  126.     
  127.     apath = (struct AnchorPath *) xmalloc (sizeof (struct AnchorPath) + PATH_SIZE);
  128.     memset(apath,0,sizeof (struct AnchorPath) + PATH_SIZE);
  129.  
  130.     {
  131.     apath->ap_Strlen = PATH_SIZE;
  132.  
  133.     if (MatchFirst (wc, apath) == 0)
  134.     {
  135.         do
  136.         {
  137.         o = variable_buffer_output (o, apath->ap_Buf,
  138.             strlen (apath->ap_Buf));
  139.         o = variable_buffer_output (o, " ",1);
  140.         } while (MatchNext (apath) == 0);
  141.     }
  142.  
  143.     MatchEnd (apath);
  144.     }
  145.     free(apath);
  146.  
  147.     return o;
  148. }
  149.  
  150. void
  151. amiga_get_global_env(void)
  152. {
  153.     BPTR env, old;
  154.     struct FileInfoBlock *fib;
  155.  
  156.     if((fib = AllocDosObject(DOS_FIB,NULL)) != NULL)
  157.     {
  158.     env = Lock ("ENV:", ACCESS_READ);
  159.     if (env)
  160.     {
  161.         old = CurrentDir (DupLock(env));
  162.         Examine (env, fib);
  163.  
  164.         while (ExNext (env, fib))
  165.         {
  166.         if (fib->fib_DirEntryType < 0) /* File */
  167.         {
  168.             /* Define an empty variable. It will be filled in
  169.             variable_lookup(). Makes startup quite a bit
  170.             faster. */
  171.             define_variable (fib->fib_FileName,
  172.                 strlen (fib->fib_FileName),
  173.             "", o_env, 1)->export = v_export;
  174.         }
  175.         }
  176.         UnLock (env);
  177.         UnLock(CurrentDir(old));
  178.     }
  179.     FreeDosObject(DOS_FIB,fib);
  180.     }
  181. }
  182.  
  183. void 
  184. amiga_set_makelevel(unsigned int makelevel)
  185. {
  186.     char buffer[20];
  187.     int len;
  188.  
  189.     len = GetVar ("MAKELEVEL", buffer, sizeof (buffer), GVF_LOCAL_ONLY);
  190.  
  191.     if (len != -1)
  192.     {
  193.     sprintf (buffer, "%u", makelevel);
  194.     SetVar ("MAKELEVEL", buffer, -1, GVF_LOCAL_ONLY);
  195.     }
  196. }
  197.  
  198. void
  199. amiga_startup(int *argc,char ***argv)
  200. {
  201.     if(*argc == 0)
  202.     {
  203.     wbmsg = (struct WBStartup *) *argv;
  204.  
  205.     if(wbmsg->sm_NumArgs > 1)
  206.     {       
  207.         char **myargv;
  208.         char *namebuf;
  209.         char *dirbuf;
  210.  
  211.         struct DiskObject *dobj;
  212.         BPTR chdir;
  213.         BPTR olddir = NULL;
  214.         int ac = 2;
  215.  
  216.         namebuf = xmalloc(PATH_SIZE * 2);
  217.         dirbuf  = namebuf + PATH_SIZE;
  218.         if((chdir   = DupLock(wbmsg->sm_ArgList[1].wa_Lock)) != NULL)
  219.         olddir = CurrentDir(chdir);
  220.  
  221.         if((dobj = GetDiskObjectNew(wbmsg->sm_ArgList[1].wa_Name)) != NULL)
  222.         {
  223.         char **tt = dobj->do_ToolTypes;
  224.  
  225.         while(*tt != NULL)
  226.         {
  227.             ac++;
  228.             tt++;
  229.         }
  230.         }
  231.  
  232.         myargv  = (char **) xmalloc((ac+1) * sizeof(char *));
  233.  
  234.         /* add any tooltype */
  235.         if(dobj != NULL)
  236.         {
  237.         char **tt = dobj->do_ToolTypes;
  238.         int ac = 2;
  239.  
  240.         while(*tt != NULL)
  241.         {
  242.             myargv[ac] = xmalloc(strlen(*tt)+1);
  243.             strcpy(myargv[ac],*tt);
  244.             ac++;
  245.             tt++;
  246.         }
  247.         FreeDiskObject(dobj);
  248.         }
  249.         if(olddir != NULL)
  250.         CurrentDir(olddir);
  251.  
  252.         /* argv[0] have to contain the program name */
  253.         myargv[0] = xmalloc(strlen(wbmsg->sm_ArgList[0].wa_Name)+1);
  254.         strcpy(myargv[0],wbmsg->sm_ArgList[0].wa_Name);
  255.  
  256.         if(chdir != NULL)
  257.         {
  258.            /* now change to the working directory */
  259.            strcpy(dirbuf,"-C");
  260.            if(NameFromLock(chdir,&dirbuf[2],PATH_SIZE-2))
  261.            {
  262.            myargv[1] = xmalloc(strlen(dirbuf)+1);
  263.            strcpy(myargv[1],dirbuf);
  264.            }
  265.            UnLock(chdir);
  266.         } else
  267.            ac--;
  268.  
  269.         /* terminate argument vector */
  270.         myargv[ac] = NULL;
  271.  
  272.         *argv = myargv;
  273.         *argc = ac;
  274.  
  275.         /* free the tempory buffer */
  276.         free(namebuf);
  277.  
  278. #ifdef DEBUG
  279.         {
  280.            int i;
  281.  
  282.            for(i = 0; i < ac ; i++)
  283.            KPrintF("myargv[%ld] = \"%s\"\n",i,myargv[i]);
  284.         }
  285. #endif
  286.     }
  287.     }
  288. }
  289.  
  290. #ifdef HAVE_GETLOADAVG
  291. #include <proto/SysInfo.h>
  292. #include <libraries/SysInfo.h>
  293. int
  294. getloadavg(double loadavg[],int nelem)
  295. {
  296.     struct Library *SysInfoBase;
  297.     int rc = -1;
  298.  
  299.     if((SysInfoBase = OpenLibrary(SYSINFONAME,SYSINFOVERSION)) != NULL)
  300.     {
  301.     struct SysInfo *sinfo;
  302.  
  303.     if((sinfo = InitSysInfo()) != NULL)
  304.     {
  305.         struct SI_LoadAverage load;
  306.  
  307.         if(sinfo->loadavg_type != LOADAVG_NONE)
  308.         {
  309.         GetLoadAverage(sinfo,&load);
  310.  
  311.         rc = 0;
  312.         switch(sinfo->loadavg_type)
  313.         {
  314.         case LOADAVG_FIXEDPNT:
  315.             if(nelem >= 0)
  316.             {
  317.             loadavg[0] = (double) load.lavg_fixed.load1 / (double) sinfo->fscale;
  318.             rc++;
  319.             }
  320.             if(nelem >= 1)
  321.             {
  322.             loadavg[1] = (double) load.lavg_fixed.load2 / (double) sinfo->fscale;
  323.             rc++;
  324.             }
  325.             if(nelem >= 2)
  326.             {
  327.             loadavg[2] = (double) load.lavg_fixed.load3 / (double) sinfo->fscale;
  328.             rc++;
  329.             }
  330.             break;
  331.         default:
  332.             rc = -1;
  333.         }
  334.         }
  335.  
  336.         FreeSysInfo(sinfo);
  337.     }
  338.     CloseLibrary(SysInfoBase);
  339.     }
  340.  
  341.     return rc;
  342. }
  343. #endif
  344.